package
{
	import controllers.VideoController;
	
	import flash.events.NetStatusEvent;
	import flash.media.Camera;
	import flash.media.Microphone;
	
	import models.vo.UserVO;
	
	import org.dexter.sendDexterEvent;
	
	public class VideoCtrl extends VideoController
	{
		public var publishName:String;
		public function VideoCtrl()
		{
			super();
		}
		[DexterEvent]
		override public function stratusInfo(event:NetStatusEvent):void{
			super.stratusInfo(event);
			switch(event.info.code){
				case "NetGroup.MulticastStream.PublishNotify":
					if(sendDexterEvent("getUserByID",event.info.name)){
						playStream(event.info.name);
						sendDexterEvent("setVideo",event.info.name);
					}
					else
						publishName = event.info.name;
				break;
			}
			sendDexterEvent("callJs","jsTrace","Stratus:"+event.info.code);
		}
		[DexterExternal]
		public function publishVideo(id:String):void{
			if(id == publishName)
				sendDexterEvent("broadcast","unPublishUserVideo",id);
			else
				sendDexterEvent("broadcast","publishUserVideo",id);
		}
		[DexterEvent]
		public function $unPublishUserVideo(id:String):void{
			publishName = null;
			if(id == UserVO.self.id){
				outStream.close();
			}
			setVideo();
		}
		[DexterEvent]
		public function $publishUserVideo(id:String):void{
			publishName = id;
			if(UserVO.self.id == id){
				publishStream();
				setVideo();
			}
		}
		[DexterEvent]
		public function userOffline(user:UserVO):void{
			if(publishName == user.id){
				publishName = null;
				setVideo();
			}
		}
		private function setVideo():void{
			sendDexterEvent("setVideo",publishName);
		}
	}
}